package com.alcatel_lucent.dms.service.generator;

import java.io.File;
import java.text.DateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Locale;

import com.alcatel_lucent.dms.BusinessException;
import com.alcatel_lucent.dms.Constants;
import com.alcatel_lucent.dms.action.ProgressQueue;
import com.alcatel_lucent.dms.model.Dictionary;
import com.alcatel_lucent.dms.model.DictionaryLanguage;

import org.apache.commons.io.FilenameUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class DictionaryGenerator {

    public static final String GEN_SIGN = "Generated by DMS";
    private static DateFormat dFmt = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.FULL, Locale.ENGLISH);

    protected Logger log = LoggerFactory.getLogger(getClass());

    public void generateDict(File target, Collection<Dictionary> dictList, GeneratorSettings settings) throws BusinessException {
        int totalLabels = 0;
        int curLabels = 0;
        int totalDict = dictList.size();
        int curDict = 1;
        for (Dictionary dict : dictList) {
            totalLabels += dict.getLabelNum();
        }
        for (Dictionary dict : dictList) {
            int percent = (int) Math.round(curLabels * 100.0 / totalLabels + .5);
            ProgressQueue.setProgress("Generating " + dict.getFormat() + " (" + curDict + "/" + totalDict + ") " + dict.getName(), percent);
            generateDict(target, dict.getId(), settings);
            curLabels += dict.getLabelNum();
            curDict++;
        }
    }

    abstract public void generateDict(File target, Long dictId, GeneratorSettings settings) throws BusinessException;

    abstract public Constants.DictionaryFormat getFormat();

    /**
     * This method generate name for those files similar to property file
     * Convert original dictionary name into proper name according its language code
     *
     * @param originalName original dictionary name
     * @param dl           Dictionary language
     * @param targetExt    target file name extension
     * @return constructed file name
     */
    protected String getFileName(String originalName, DictionaryLanguage dl, String targetExt) {
        String oriExt = FilenameUtils.getExtension(originalName);
        String baseName = FilenameUtils.removeExtension(originalName);
        if (null == dl) return baseName + FilenameUtils.EXTENSION_SEPARATOR + targetExt;
        int pos = baseName.lastIndexOf(dl.getDictionary().getReferenceLanguage());
        String name = baseName.endsWith(dl.getDictionary().getReferenceLanguage()) ? baseName.substring(0, baseName.length() - dl.getDictionary().getReferenceLanguage().length()) : baseName + "_";
        name += dl.getLanguageCode() + FilenameUtils.EXTENSION_SEPARATOR + targetExt;
        return name;
    }

    /***/
    public static String getDMSGenSign() {
        return GEN_SIGN + " at " + dFmt.format(new Date());
    }
}
